package com.ljs.view.world
{
	import com.ljs.control.MainContent;
	import com.ljs.define.CharStateDefine;
	import com.ljs.define.CharTypeDefine;
	import com.ljs.view.map.Map;
	import com.ljs.view.role.Role;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import skyExt.SkyMath;

	public class WorldDynObj extends MovieClip
	{
		private var _map:Map;
		private var _role:Role;
		private var _content:MainContent;
		private var _targetPoint:Array=new Array(2);
		private var _mv:Boolean=false;
		private static const speed_x:int=4;
		private static const speed_y:int=4;
		public function WorldDynObj()
		{
			init();
		}
		
		private function init()
		{
			_map=new Map();
			addChild(_map);
			_role=new Role();
			addChild(_role);
			_map.addEventListener(MouseEvent.CLICK,onMouseEvent);
			addEventListener(Event.ENTER_FRAME,onFrame);
		}
		
		private function onFrame(e:Event)
		{
			if(_mv)
			{
				if(_role.x>_targetPoint[0])
				{
					_role.x=_role.x-speed_x;
					_role.setState(CharStateDefine.Z_MV);
					if(_role.x<=_targetPoint[0])
					{
						_role.x=_targetPoint[0];
					}
				}
				else if(_role.x<_targetPoint[0])
				{
					_role.x=_role.x+speed_x;
					_role.setState(CharStateDefine.Y_MV);
					if(_role.x>=_targetPoint[0])
					{
						_role.x=_targetPoint[0];
					}
				}
			
				if(_role.y>_targetPoint[1])
				{
					_role.y=_role.y-speed_y;
					_role.yZL();
					if(_role.y<=_targetPoint[1])
					{
						_role.y=_targetPoint[1];
					}
				}
				else if(_role.y<_targetPoint[1])
				{
					_role.y=_role.y+speed_y;
					_role.yZL();
					if(_role.y>=_targetPoint[1])
					{
						_role.y=_targetPoint[1];
					}
				}
				
				if(_role.x==_targetPoint[0]&&_role.y==_targetPoint[1])
				{
					_mv=false;
					_role.dj();
				}
			}
		}
		
		private function onMouseEvent(e:MouseEvent)
		{
			_mv=true;
			_targetPoint[0]=e.stageX;
			_targetPoint[1]=e.stageY;
		}
		
		public function setContent(d:MainContent)
		{
			_content=d;
			_map.setContent(d);
			_role.setContent(d);
		}
		
		public function create()
		{
			_role.create();
			_role.x=_content.worldVo.startX;
			_role.y=_content.worldVo.startY;
			_map.create();
		}
		
		public function destroy()
		{
			_role.destroy();
			_map.destroy();
		}
	}
}